package com.tre.jdevtemplateboot.common.util;

import com.tre.jdevtemplateboot.common.dto.ReqRestEntity;
import com.tre.jdevtemplateboot.common.dto.ReqRestHeaderEntity;
import com.tre.jdevtemplateboot.common.dto.ResponseResultVO;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @author JDev
 */
public class RestTemplateUtils {

    private static RestTemplate client = new RestTemplate();
    private static HttpHeaders headers = new HttpHeaders();

    private RestTemplateUtils() {
        throw new UnsupportedOperationException();
    }


    public static HttpHeaders sendPostLoginRequest(ReqRestEntity reqRestEntity) {

        headers.setContentType(reqRestEntity.getContentType());
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(reqRestEntity.getParams(), headers);
        return client.exchange(reqRestEntity.getUri(), HttpMethod.POST, requestEntity, ReqRestHeaderEntity.class).getHeaders();
    }


    public static ResponseResultVO sendPostTokenRequest(ReqRestEntity reqRestEntity) {

        headers.setContentType(reqRestEntity.getContentType());
        headers.setBearerAuth(reqRestEntity.getToken());
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(reqRestEntity.getParams(), headers);
        return client.exchange(reqRestEntity.getUri(), HttpMethod.POST, requestEntity, ResponseResultVO.class).getBody();
    }

    public static ResponseResultVO sendPostRequest(ReqRestEntity reqRestEntity) {

        headers.setContentType(reqRestEntity.getContentType());
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(reqRestEntity.getParams(), headers);
        return client.exchange(reqRestEntity.getUri(), HttpMethod.POST, requestEntity, ResponseResultVO.class).getBody();
    }

}
